﻿using System.Security.Policy;
using RiskMan.DataClass.Models;
using RiskMan.DataClass.Models.Sterling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TheControllerRisk.Settings;
using RiskMan.Views.Dialogs;

namespace RiskMan.Views.Streling
{
    /// <summary>
    /// Interaction logic for AddSterlingRobot.xaml
    /// </summary>
    public partial class AddSterlingRobot : Window
    {
        private SterlingItem SItem;
        private DataClass.Data data;
        List<Trade> trades;

        public AddSterlingRobot(SterlingItem sItem, DataClass.Data data)
        {
            InitializeComponent();

            SItem = sItem;
            this.data = data;
            trades = data.GetTradeList();


            cmbx_ClientCode.ItemsSource = trades.Select(a => a.ClientCode).ToList();
            for (int i = 0; i < cmbx_ClientCode.Items.Count; i++)
            {
                if (cmbx_ClientCode.Items[i].ToString() == sItem.ClientCode)
                    cmbx_ClientCode.SelectedIndex = i;
            }
            cmbx_TypeDeal.ItemsSource = new List<string>() { cfg.Buy, cfg.Sell };
            if (SItem.Operation != null)
                cmbx_TypeDeal.SelectedValue = SItem.Operation;

            

            txbxQuantity.Text = sItem.Quantity.ToString();
            #region Seccode
            //cmbx_Instrument.ItemsSource = data.GetSeccodeList();
            //for (int i = 0; i < cmbx_Instrument.Items.Count; i++)
            //{
            //    if (cmbx_Instrument.Items[i].ToString() == sItem.Instrument)
            //        cmbx_Instrument.SelectedIndex = i;
            //}

            cmbx_Instrument.ItemsSource = data.SterlingSymbols.Select(a => a.Symbol).ToList();
            for (int i = 0; i < cmbx_Instrument.Items.Count; i++)
            {
                if (cmbx_Instrument.Items[i].ToString() == sItem.Instrument)
                    cmbx_Instrument.SelectedIndex = i;
            }
            #endregion

            CmbxECN.ItemsSource = data.DictionaryEcn.Select(a => a.Value).ToList();
            //comment

            TxbxPriceOpen.Text = SItem.PriceOpen.ToString();
            txbxDevation.Text = SItem.Deviation.ToString();
            TxbxDeviationStartRobot.Text = SItem.DeviationStartRobot.ToString();

            TxbxInitialStop.Text = SItem.InitialStop.ToString();
            TxbxBreakeven.Text = SItem.Breakeven.ToString();
            TxbxPriceClosePoz.Text = SItem.PriceClosePoz.ToString();
            TxbxMoveStop.Text = SItem.MoveStop.ToString();
            TxbxChangePrice2.Text = SItem.ChangePrice2.ToString();

            TxbxTimeCancelRepeatOpen.Text = SItem.TimeCancelRepeatOpen.TimeOfDay.ToString();
            TxbxTimeKillAndClosePoz.Text = SItem.TimeKillAndClosePoz.TimeOfDay.ToString();


        }

        #region Обработка получение текстобксом фокуса и потери его, замена запятой на точку

        private void TxbxLostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = 0.ToString();

            char pointChar = ',';
            string str = (1.2).ToString();
            if (str.IndexOf('.') > 0) pointChar = '.';

            if (pointChar == ',')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace('.', pointChar);

            if (pointChar == '.')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(',', pointChar);


        }

        private void TxbxGotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (((TextBox)sender).Text != "")
                {
                    if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                    {
                        ((TextBox)sender).Text = "";
                    }
                }
            }
            catch
            {
                ((TextBox)sender).Text = "";
            }
        }

        private void TxbxPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;

            if ((e.Text.IndexOf('.') == 0 || e.Text.IndexOf(',') == 0) && ((TextBox)sender).Text.IndexOf('.') == -1 && ((TextBox)sender).Text.IndexOf(',') == -1)
                e.Handled = false;
        }

        private void TxbxDitgitPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;
        }
        #endregion

        private void Button_Accept_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (cmbx_ClientCode.SelectedIndex >= 0)
                {
                    SItem.ClientCode = cmbx_ClientCode.SelectedValue.ToString();
                    List<Trade> trades = data.GetTradeList();

                    for (int i = 0; i < trades.Count; i++)
                        if (trades[i].ClientCode == SItem.ClientCode)
                            SItem.Account = trades[i].Account;
                }
                else
                {
                    new DialogOkCancel("Выберите код клиента(счет)!", "Ошибка добавления");
                    return;
                }

                if (cmbx_TypeDeal.SelectedIndex >= 0)
                    SItem.Operation = cmbx_TypeDeal.SelectedValue.ToString();
                else
                {
                    new DialogOkCancel("Выберите вид операции!", "Ошибка добавления");
                    return;
                }

                if (cmbx_Instrument.SelectedIndex >= 0)
                    if (cmbx_Instrument.SelectedValue.ToString() != "Не выбран")
                        SItem.Instrument = cmbx_Instrument.SelectedValue.ToString();
                    else
                    {
                        new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                        return;
                    }
                else
                {
                    new DialogOkCancel("Выберите торгуемый инструмент!", "Ошибка добавления");
                    return;
                }
                SItem.Quantity = Convert.ToInt32(txbxQuantity.Text);

                SItem.PriceOpen = Convert.ToDouble(TxbxPriceOpen.Text);
                SItem.DeviationStartRobot = Convert.ToDouble(TxbxDeviationStartRobot.Text);


                SItem.InitialStop = Convert.ToDouble(TxbxInitialStop.Text);
                SItem.PriceClosePoz = Convert.ToDouble(TxbxPriceClosePoz.Text);
                SItem.Breakeven = Convert.ToDouble(TxbxBreakeven.Text);
                SItem.MoveStop = Convert.ToDouble(TxbxMoveStop.Text);
                SItem.ChangePrice2 = Convert.ToDouble(TxbxChangePrice2.Text);

                SItem.TimeCancelRepeatOpen = Convert.ToDateTime(TxbxTimeCancelRepeatOpen.Text);
                SItem.TimeKillAndClosePoz = Convert.ToDateTime(TxbxTimeKillAndClosePoz.Text);

                DialogResult = true;
                Close();
            }
            catch
            {
            }

        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}

